
#include "SmartMaterialExtensions.h"
//#include "Map.h"
#include "Engine/Texture.h"


TArray<class UMaterialExpression*> USmartMaterialExtensions::SmGetAllExpressions( UMaterial* Material)
{
	return Material->Expressions;
}

TMap<FString, FString> USmartMaterialExtensions::SmListTextures(UMaterial * Material)
{
	TMap<FString, FString> MaterialTextures;
	TArray<const UMaterialExpressionTextureBase*> Result;
	Material->GetAllExpressionsOfType(Result);
	int32 Num = Result.Num();
	if (Num > 0) {
		for (const UMaterialExpressionTextureBase* Texture : Result) {
			if (!MaterialTextures.Contains(Texture->GetName())) {
				MaterialTextures.Add(Texture->GetName(), Texture->Texture->GetPathName());
			}
		}
	}
	return MaterialTextures;
}

void USmartMaterialExtensions::SmSetTextures(UMaterial* Material, TMap<FString, FString> Data)
{
	TArray<UMaterialExpressionTextureBase*> Result;
	Material->GetAllExpressionsInMaterialAndFunctionsOfType(Result);
	int32 Num = Result.Num();
	if (Num > 0) {
		for (UMaterialExpressionTextureBase* ExpTexture : Result) {
			if (Data.Contains(ExpTexture->GetName())) {
				FString TexturePath = Data[ExpTexture->GetName()];
				UTexture* Texture = Cast<UTexture>(StaticLoadObject(UTexture::StaticClass(), NULL, *TexturePath));
				if (Texture) {
					ExpTexture->Texture = Texture;
				}
			}
		}
	}
}
